var check = document.querySelector('#check')
var sub = document.querySelector('#sub')
var form = document.querySelector('form')
var u1 = document.querySelector('[name="username"]')
var p1 = document.querySelector('[name="password"]')
    // 复选框选中 登录才能点击
check.onclick = function() {
    if (this.checked) {
        sub.disabled = false
    } else {
        sub.disabled = true
    }
}

// 判断是否有参数 地址？号后即为参数
var search = location.search
    // 给表单绑定提交事件
form.onsubmit = function() {
    // 获取用户名 密码的值
    var uname = u1.value;
    var upass = p1.value;
    (async function() {
        var p1 = await PromiseAjax({
            url: './php/login.php',
            data: `username=${uname}&password=${upass}`
        })
        if (p1 == 1) {
            //设置cookie
            setCookie('name1', uname)
                //判断地址栏中是否有参数 这个参数是传来的购物车地址
            if (search) {
                //获取地址栏中的参数
                // console.log(location)
                var data = location.search
                    //分割跳转地址 pathurl=http://localhost/clean/buy.html
                var url = data.split('=')[1]
                    //把获取的跳转地址赋值给location对象
                location = url //url即为等号后地址
            } else {
                location = './shouye.html'
            }
        } else {
            alert("登录失败，请重新登录")
        }
    })()
    return false
}